package com.itbaizhan.shopping_category_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_category_service.mapper.CategoryMapper;
import com.itbaizhan.shopping_common.pojo.Category;
import com.itbaizhan.shopping_common.service.CategoryService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Arrays;
import java.util.List;
@DubboService
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    // 对象名必须叫redisTemplate，否则由于容器中有多个RedisTemplate对象造成无法注入
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 添加广告
     * @param category
     */
    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
        refreshRedisCategory();
    }

    /**
     * 修改广告
     * @param category
     */
    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
        refreshRedisCategory();
    }

    /**
     * 删除广告
     * @param ids
     */
    @Override
    public void delete(Long[] ids) {
      categoryMapper.deleteBatchIds(Arrays.asList(ids));
        refreshRedisCategory();
    }

    /**
     * 修改广告状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        Category category = categoryMapper.selectById(id);
        category.setStatus(status);
        categoryMapper.updateById(category);
        refreshRedisCategory();
    }

    /**
     * 根据id查询广告
     * @param id
     * @return
     */
    @Override
    public Category findById(Long id) {
        return categoryMapper.selectById(id);
    }

    /**
     * 分页查询广告
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Category> search(Integer page, Integer size) {
      return   categoryMapper.selectPage(new Page<>(page,size),null);
    }

    /**
     * 查询出所有状态为开启的广告
     * @return
     */
    @Override
    public List<Category> findAll() {
      //1. 现在redis中查询广告数据
      //1.1 获取操作redis中list数据的对象
        ListOperations<String, Category> listOperations = redisTemplate.opsForList();
       //1.2 从redis中获取所有启用的广告
        List<Category> categoryList = listOperations.range("categories", 0, -1);

    if (categoryList != null && categoryList.size() > 0){
        //2.如果查询到广告数据，则直接返回
        return categoryList;
    }else {
        //3. redis中不存在广告数据，则在数据库中查询广告数据
        QueryWrapper<Category> queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",1);
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        //将数据同步到redis中
        listOperations.rightPushAll("categories",categories);
        return categories;
    }
    }

    /**
     * 更新redis中的广告数据
     */
  public void  refreshRedisCategory(){
      //从数据库中查询广告
      QueryWrapper<Category> queryWrapper = new QueryWrapper();
      queryWrapper.eq("status",1);
      List<Category> categories = categoryMapper.selectList(queryWrapper);
      //删除redis中原有的广告数据
       redisTemplate.delete("categories");
      //将新的广告数据同步到redis中
      // 获取操作redis中list数据的对象
      ListOperations<String, Category> listOperations = redisTemplate.opsForList();
      listOperations.rightPushAll("categories",categories);
  }

}
